//
//  NSAttributedString+Extension.swift
//  Weibo
//
//  Created by Apple on 2017/7/17.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit

extension NSAttributedString {
    //  根据表情模型和字体对象创建富文本
    class func attributedStringWithEmoticon(emoticon: HMEmoticon, font: UIFont) -> NSAttributedString {
        //  1. 创建图片对象
        let image = UIImage(named: emoticon.path!, in: emoticonBundle, compatibleWith: nil)!
        //  2. 创建文本附件
        let attachment = HMTextAttachment()
        //  绑定模型
        attachment.emoticon = emoticon
        //  3. 设置文本附件对应的图片
        attachment.image = image
        //  获取文字高度
        let lineHeight = font.lineHeight
        //  设置bounds
        attachment.bounds = CGRect(x: 0, y: -3.5, width: lineHeight, height: lineHeight)
        //  4. 根据文本附件创建富文本
        let attributedText = NSAttributedString(attachment: attachment)
        return attributedText
    }
}
